package controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import pojo.DoctorWork;
import pojo.WorkPlan;
import service.DoctorWorkService;
import service.WorkPlanService;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/work-plan")
@Api(tags = "医院工作计划与排班管理接口")
public class WorkPlanController {

    @Autowired
    private DoctorWorkService doctorWorkService;

    @Autowired
    private WorkPlanService workPlanService;

    /* 医生排班相关接口 */

    @GetMapping("/getDoctorWorksByDay/{day}")
    @ApiOperation(value = "获取某天的医生排班信息", notes = "根据日期查询当天所有医生的排班情况")
    @ApiImplicitParam(name = "day", value = "日期，格式yyyy-MM-dd", required = true, dataType = "String", paramType = "path")
    public ResponseEntity<List<DoctorWork>> getDoctorWorkByDay(
            @PathVariable String day) {
        List<DoctorWork> result = doctorWorkService.getDoctorWorkByDay(day);
        return ResponseEntity.ok(result);
    }

    @PutMapping("/decreaseCountLeft/{day}/{docId}")
    @ApiOperation(value = "减少可挂号数量", notes = "当有患者挂号时调用此接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "day", value = "日期，格式yyyy-MM-dd", required = true, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "docId", value = "医生ID", required = true, dataType = "String", paramType = "path")
    })
    public ResponseEntity<Boolean> decreaseDoctorCount(
            @PathVariable String day,
            @PathVariable String docId) {
        boolean result = doctorWorkService.decreaseCountLeft(day, docId);
        return result ? ResponseEntity.ok(true) : ResponseEntity.badRequest().build();
    }

    @PutMapping("/increaseCountLeft/{day}/{docId}")
    @ApiOperation(value = "增加可挂号数量", notes = "当取消挂号时调用此接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "day", value = "日期，格式yyyy-MM-dd", required = true, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "docId", value = "医生ID", required = true, dataType = "String", paramType = "path")
    })
    public ResponseEntity<Boolean> increaseDoctorCount(
            @PathVariable String day,
            @PathVariable String docId) {
        boolean result = doctorWorkService.increaseCountLeft(day, docId);
        return result ? ResponseEntity.ok(true) : ResponseEntity.badRequest().build();
    }

    /* 工作计划相关接口 */

    @GetMapping("/getPlansStatus/{year}/{month}/{day}")
    @ApiOperation(value = "获取指定日期的工作计划", notes = "根据年、月、日查询工作计划")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "year", value = "年份", required = true, dataType = "int", paramType = "path"),
            @ApiImplicitParam(name = "month", value = "月份", required = true, dataType = "int", paramType = "path"),
            @ApiImplicitParam(name = "day", value = "日", required = true, dataType = "int", paramType = "path")
    })
    public ResponseEntity<WorkPlan> getWorkPlanByDate(
            @PathVariable int year,
            @PathVariable int month,
            @PathVariable int day) {
        WorkPlan result = workPlanService.getWorkPlanByDate(year, month, day);
        return ResponseEntity.ok(result);
    }

    @PutMapping("/updatePlanStatus/{day}/{status}")
    @ApiOperation(value = "更新工作计划状态", notes = "修改指定日期的工作计划状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "day", value = "日期，格式yyyy-MM-dd", required = true, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "status", value = "状态(0:未启用,1:已启用)", required = true, dataType = "int", paramType = "path")
    })
    public ResponseEntity<Boolean> updateWorkPlanStatus(
            @PathVariable String day,
            @PathVariable int status) {
        boolean result = workPlanService.updateWorkPlanStatus(day, status);
        return result ? ResponseEntity.ok(true) : ResponseEntity.badRequest().build();
    }

    @GetMapping("/getPlansByWeek/{week}")
    @ApiOperation(value = "获取一周的工作计划", notes = "根据周次查询工作计划")
    @ApiImplicitParam(name = "week", value = "周次日期字符串，格式为'yyyy-MM-dd,yyyy-MM-dd,...'", required = true, dataType = "String", paramType = "path")
    public ResponseEntity<List<DoctorWork>> getWeeklyWorkPlan(
            @PathVariable String week) {
        List<DoctorWork> result = workPlanService.getWeeklyWorkPlan(week);
        return ResponseEntity.ok(result);
    }

    @PostMapping("/generatePlans")
    @ApiOperation(value = "创建工作计划", notes = "新增一个工作计划记录")
    public ResponseEntity<Boolean> createWorkPlan(
            @RequestBody @Valid WorkPlan workPlan) {
        boolean result = workPlanService.createWorkPlan(workPlan);
        return result ? ResponseEntity.ok(true) : ResponseEntity.badRequest().build();
    }
    // 新增：查询医生出诊详情接口
    @GetMapping("/work-details")
    @ApiOperation(value = "查询医生出诊详情", notes = "查询所有医生的出诊详细信息，包含科室、时间等")
    public ResponseEntity<List<Map<String, Object>>> selectWorkDetails() {
        List<Map<String, Object>> result = workPlanService.selectWorkDetails();
        return ResponseEntity.ok(result);
    }
    @GetMapping("/doctor-work-details/{doctorId}")
    @ApiOperation(value = "根据医生ID查询出诊详情", notes = "查询指定医生的出诊信息，包含出诊日期、时间、诊室等")
    @ApiImplicitParam(name = "doctorId", value = "医生ID", required = true, dataType = "String", paramType = "path")
    public ResponseEntity<List<Map<String, Object>>> selectDoctorWorkById(
            @PathVariable String doctorId) {
        List<Map<String, Object>> result = workPlanService.selectDoctorWorkById(doctorId);
        return ResponseEntity.ok(result);
    }
    @DeleteMapping("/doctor-work/{scheduleId}")
    @ApiOperation(value = "删除医生排班", notes = "根据排班ID删除 doctor_work 表中的排班记录")
    @ApiImplicitParam(name = "scheduleId", value = "排班ID", required = true, dataType = "String", paramType = "path")
    public ResponseEntity<Boolean> deleteDoctorWork(
            @PathVariable String scheduleId) {
        boolean result = workPlanService.deleteDoctorWork(scheduleId);
        return result ? ResponseEntity.ok(true) : ResponseEntity.badRequest().build();
    }
    @PutMapping("/updateDoctorWork")
    @ApiOperation(value = "更新医生排班", notes = "根据排班ID（scheduleId）更新医生排班信息")
    public ResponseEntity<Boolean> updateDoctorWork(@RequestBody Map<String, Object> params) {
        // 校验关键参数
        List<String> requiredParams = Arrays.asList("scheduleId", "doctorId", "morningWorkStatus", "afternoonWorkStatus", "feeId", "appointmentRuleId");
        for (String param : requiredParams) {
            if (params.get(param) == null) {
                return ResponseEntity.badRequest().body(false);
            }
        }
        boolean result = workPlanService.updateDoctorWork(params);
        return result ? ResponseEntity.ok(true) : ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    @GetMapping("/getDoctorWorkByDayAndRoomId/{day}/{roomId}")
    public ResponseEntity<List<Map<String, Object>>> getDoctorWorkByDayAndRoomId(@PathVariable String day, @PathVariable Integer roomId) {
        List<Map<String, Object>> result = workPlanService.getDoctorWorkByDayAndRoomId(day, roomId);
        return ResponseEntity.ok(result);
    }
    @GetMapping("/getWeekPlanByDoctor")
    @ApiOperation(value = "医生查询排班情况", notes = "跟据医生编号查询排班")
    public ResponseEntity<List<WorkPlan>> getWeekPlanByDoctor(
            @RequestParam List<String> days,
            @RequestParam String doctorId
    ) {
        List<WorkPlan> result = workPlanService.getWeekPlanByDoctor(days, doctorId);
        return ResponseEntity.ok(result);
    }
}
